/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slsBeans;

import EL.DiningRooms;
import EL.RoomTypes;
import EL.Restaurants;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.*;

/**
 *
 * @author Tian
 */
@Stateless
public class RoomBLO implements RoomBLORemote
{

    @PersistenceContext(unitName = "Tunga_EJB3PU")
    private EntityManager em;

    public List<DiningRooms> getAll()
    {
        String hql = "FROM DiningRooms";
        Query query = this.em.createQuery(hql);
        List<DiningRooms> rooms = query.getResultList();
        return rooms;
    }

    public DiningRooms getByID(int id)
    {
        DiningRooms room = em.find(DiningRooms.class, id);
        return room;
    }

    public boolean add(DiningRooms room, RoomTypes roomType, Restaurants restaurant)
    {
        List<DiningRooms> rooms = new ArrayList<DiningRooms>();
        rooms.add(room);

        room.setRoomTypes(roomType);
        room.setRestaurants(restaurant);
        restaurant.setDiningRoomsList(rooms);
        roomType.setDiningRoomsList(rooms);
        try
        {
            this.em.merge(room);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public boolean edit(DiningRooms room)
    {
        try
        {
            this.em.merge(room);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
    public boolean delete(int id)
    {
        DiningRooms room = getByID(id);
        if(room != null)
        {
            try
            {
                this.em.remove(room);
                return true;
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
